package com.iwomy.envpro.platform.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.envpro.common.core.util.R;
import com.iwomy.envpro.common.log.annotation.SysLog;
import com.iwomy.envpro.platform.VO.BasicSmsConfigVO;
import com.iwomy.envpro.platform.VO.BasicTelephoneConfigVO;
import com.iwomy.envpro.platform.entity.BasicSmsConfigEntity;
import com.iwomy.envpro.platform.entity.BasicTelephoneConfigEntity;
import com.iwomy.envpro.platform.service.BasicTelephoneConfigService;
import com.iwomy.envpro.platform.utils.DeptFillUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.envpro.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 基础信息-语音配置
 *
 * @author pig
 * @date 2023-11-22 18:05:05
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicTelephoneConfig")
@Tag(description = "basicTelephoneConfig", name = "基础信息-语音配置管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicTelephoneConfigController {

	private final BasicTelephoneConfigService basicTelephoneConfigService;

	/**
	 * 分页查询
	 *
	 * @param page                 分页对象
	 * @param basicTelephoneConfig 基础信息-语音配置
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_basicTelephoneConfig_view')")
	public R getBasicTelephoneConfigPage(@ParameterObject Page page, @ParameterObject BasicTelephoneConfigEntity basicTelephoneConfig) {
		LambdaQueryWrapper<BasicTelephoneConfigEntity> wrapper = Wrappers.lambdaQuery();
		page = basicTelephoneConfigService.page(page, wrapper);
		DeptFillUtils.fill(page);
		return R.ok(page);
	}


	/**
	 * 通过id查询基础信息-语音配置
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_basicTelephoneConfig_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(BeanUtil.copyProperties(basicTelephoneConfigService.getById(id), BasicTelephoneConfigVO.class));
	}

	/**
	 * 新增基础信息-语音配置
	 *
	 * @param basicTelephoneConfig 基础信息-语音配置
	 * @return R
	 */
	@Operation(summary = "新增基础信息-语音配置", description = "新增基础信息-语音配置")
	@SysLog("新增基础信息-语音配置")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_basicTelephoneConfig_add')")
	public R save(@RequestBody BasicTelephoneConfigEntity basicTelephoneConfig) {

		LambdaQueryWrapper<BasicTelephoneConfigEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(BasicTelephoneConfigEntity::getStatus, 20);
		if (basicTelephoneConfig.getStatus() == 20 && basicTelephoneConfigService.count(queryWrapper) > 0) {
			return R.failed("有开启的语音接口正在运行，请先关闭");
		}

		return R.ok(basicTelephoneConfigService.save(basicTelephoneConfig));
	}

	/**
	 * 修改基础信息-语音配置
	 *
	 * @param basicTelephoneConfig 基础信息-语音配置
	 * @return R
	 */
	@Operation(summary = "修改基础信息-语音配置", description = "修改基础信息-语音配置")
	@SysLog("修改基础信息-语音配置")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_basicTelephoneConfig_edit')")
	public R updateById(@RequestBody BasicTelephoneConfigEntity basicTelephoneConfig) {

		LambdaQueryWrapper<BasicTelephoneConfigEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(BasicTelephoneConfigEntity::getStatus, 20);
		BasicTelephoneConfigEntity basicSmsConfigEntity = basicTelephoneConfigService.getOne(queryWrapper);
		if (ObjectUtil.isNotNull(basicSmsConfigEntity) && !basicSmsConfigEntity.getId().equals(basicTelephoneConfig.getId())
				&& basicTelephoneConfig.getStatus() == 20) {
			return R.failed("有开启的语音接口正在运行，请先关闭");
		}
		return R.ok(basicTelephoneConfigService.updateById(basicTelephoneConfig));
	}

	/**
	 * 通过id删除基础信息-语音配置
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除基础信息-语音配置", description = "通过id删除基础信息-语音配置")
	@SysLog("通过id删除基础信息-语音配置")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_basicTelephoneConfig_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(basicTelephoneConfigService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param basicTelephoneConfig 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_basicTelephoneConfig_export')")
	public List<BasicTelephoneConfigEntity> export(BasicTelephoneConfigEntity basicTelephoneConfig) {
		return basicTelephoneConfigService.list(Wrappers.query(basicTelephoneConfig));
	}
}